<template>
  <div>
    <el-card class="box-card">
      <el-row>
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">系统管理</el-breadcrumb-item>
          <el-breadcrumb-item>区域管理</el-breadcrumb-item>
        </el-breadcrumb>
      </el-row>
      <el-row><br><br></el-row>
      <el-row>
        <el-col :span="1"><br></el-col>
        <el-col :span="5">
          <el-input v-model="dto.name" placeholder="请输入内容"></el-input>
        </el-col>
        <el-col :span="1">
          <el-button type="primary" icon="el-icon-search" @click="getRegionList()">搜索</el-button>
        </el-col>
        <el-col :span="5">
          <el-button type="success" round @click="HandleAdd()">添加区域</el-button>
        </el-col>
      </el-row>
      <el-row><br><br></el-row>
      <el-row>
        <el-col :span="1"><br></el-col>
        <el-col :span="17">
          <el-table
            :data="regionList"
            style="width: 100%">
            <el-table-column
              label="序号"
              type="index"
              width="50">
            </el-table-column>
            <el-table-column
              prop="name"
              label="区域名"
              width="300">
            </el-table-column>
            <el-table-column
              label="状态"
              width="120">
              <template slot-scope="scope">
                <span style="color: green;" v-if="scope.row.status == 1">启用</span>
                <span style="color: red;" v-else>停用</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="300">
              <template slot-scope="scope">
                <el-button
                  size="mini" type="primary" icon="el-icon-edit"
                  @click="handleEdit(scope.row)">编辑</el-button>
                <el-button
                  size="mini" v-if="scope.row.status == 1" type="warning"
                  @click="handleDisable(scope.row)">停用</el-button>
                <el-button
                  size="mini" v-if="scope.row.status == 0" type="success"
                  @click="handleEnable(scope.row)">启用</el-button>
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="1"><br></el-col>
        <el-col :span="5">
          <el-pagination
            @current-change="handleCurrentChange"
            :current-page.sync="dto.page"
            :page-size="5"
            layout="total, prev, pager, next"
            :total="total">
          </el-pagination>
        </el-col>
      </el-row>
    </el-card>
    <!-- 添加区域的表单弹出框 -->
    <el-dialog :title="formtitle" :visible.sync="dialogRegionVisible" width="30%" @close="close('ruleForm')">
      <el-form :model="region" :rules="rules" ref="ruleForm">
        <el-form-item label="区域" :label-width="formLabelWidth" prop="names">
          <el-cascader
          v-model="region.names"
          :props="{ value: 'label',label: 'label',children: 'children',expandTrigger: 'hover'}"
          :options="provinceList"
          placeholder="请选择省市区"
          clearable
          :style="{ width: '100%' }"
          @change="handleChange"></el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="addRegion('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { regionData } from 'element-china-area-data';
export default {
  data() {
    var checkName = (rule, value, callback) => {
      if (value.length == 0) {
        callback(new Error('请选择区域名称'));
      } else {
        if (this.regionName === (value[0] + ' ' + value[1] + ' ' + value[2])) {
          callback(new Error('您选择的区域未改变'));
          return
        }
        this.$axios.get("/region/checkName",{
        params:{
          name: value[0] + ' ' + value[1] + ' ' + value[2]
        }}).then(res=>{
          if (res.data.code == 200) {
            if (res.data.data) {
              callback();
            } else {
              callback(new Error('您选择的区域已存在'));
            }
          }
        })
      }
    }
    return{
      region:{
        id:"",
        names:[]
      },
      regionName:"",
      dto:{
        name:"",
        page:1,
        size:5
      },
      formLabelWidth:"40px",
      formtitle:"",
      total:"",
      provinceList:regionData,
      dialogRegionVisible:false,
      rules: {
        names: [
          { validator:checkName, trigger: 'change' }
        ]
      },
      regionList:[]
    }
  },
  mounted(){
    this.getRegionList()
  },
  methods:{
    handleChange(){
      
    },
    handleCurrentChange(val){
      this.dto.page = val
      this.getRegionList()
    },
    close(formName){
      this.$refs[formName].clearValidate();
      this.dialogRegionVisible = false
    },
    HandleAdd(){
      this.region = {
        id:"",
        names:[]
      },
      this.dialogRegionVisible = true
      this.formtitle = "添加区域"
    },
    handleEdit(row){
      this.dialogRegionVisible = true
      this.formtitle = "编辑区域"
      this.regionName = row.name
      this.region.names = row.names
      this.region.id = row.id
    },
    addRegion(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.formtitle == '添加区域') {
            this.$axios.post('/region/addRegion',this.region).then(res=>{
              if (res.data.code == 200) {
                this.$message({
                  message: '添加成功！',
                  type: 'success'
                });
                this.dialogRegionVisible = false
                this.getRegionList()
              }
            })
          }
          if (this.formtitle == '编辑区域') {
            this.$axios.put('/region/editRegion',this.region).then(res=>{
              if (res.data.code == 200) {
                this.$message({
                  message: '编辑成功！',
                  type: 'success'
                });
                this.dialogRegionVisible = false
                this.getRegionList()
              }
            })
          }     
        } else {
          return false;
        }
      }); 
    },
    getRegionList(){
      this.$axios.get('/region/getRegionList',{
        params:{
          name: this.dto.name,
          page: this.dto.page,
          size: this.dto.size
        }}).then(res=>{
        if (res.data.code == 200) {
          this.regionList = res.data.data.data
          this.total = res.data.data.total
        }
      })
    },
    handleEnable(row){
      this.$axios.put('/region/enableRegion/' + row.id).then(res=>{
        if (res.data.code == 200) {
          this.$message({
            type: 'success',
            message: '启用成功!'
          });
          this.getRegionList()
        }
      })
    },
    handleDisable(row){
      this.$axios.put('/region/disableRegion/' + row.id).then(res=>{
        if (res.data.code == 200) {
          if (res.data.data) {
            this.$message({
              type: 'success',
              message: '停用成功!'
            });
            this.getRegionList()
          } else {
            this.$message.error('该区域下存在供应商或客户，无法停用');
          }          
        }
      })
    },
    handleDelete(row){
      this.$confirm('此操作将永久删除该区域, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.delete('/region/deleteRegion/' + row.id).then(res=>{
          if (res.data.code == 200) {
            if (res.data.data){
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              this.getRegionList()
            } else {
              this.$message.error('该区域下存在供应商或客户，无法删除');
            }  
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    }
  }
}
</script>
<style scoped>
.box-card {
  height: 79vh;
}
.el-cascader {
  width: 80%; /* 设置一个具体的宽度 */
}
</style>